#include "StdAfx.h"
#include "Simulator.h"

#include "btBulletDynamicsCommon.h"

void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC)
{
	PIXELFORMATDESCRIPTOR pfd;
	int format;

	// get the device context (DC)
	*hDC = GetDC( hWnd );

	// set the pixel format for the DC
	ZeroMemory( &pfd, sizeof( pfd ) );
	pfd.nSize = sizeof( pfd );
	pfd.nVersion = 1;
	pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
	pfd.iPixelType = PFD_TYPE_RGBA;
	pfd.cColorBits = 24;
	pfd.cDepthBits = 16;
	pfd.cStencilBits = 1;
	pfd.iLayerType = PFD_MAIN_PLANE;
	format = ChoosePixelFormat( *hDC, &pfd );
	SetPixelFormat( *hDC, format, &pfd );

	// create and enable the render context (RC)
	*hRC = wglCreateContext( *hDC );
	wglMakeCurrent( *hDC, *hRC );

	//sOpenGLInitialized = true;
}

// Disable OpenGL

void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC)
{
	//sOpenGLInitialized = false;

	wglMakeCurrent( NULL, NULL );
	wglDeleteContext( hRC );
	ReleaseDC( hWnd, hDC );
}

Simulator::Simulator(void)
	: m_simulation(NULL)
	, m_hWnd(NULL)
	, m_hDC(NULL)
	, m_hRC(NULL)
{
}

Simulator::~Simulator(void)
{
}

bool Simulator::init(HWND hWnd, ISimulation * simulation)
{
	ASSERT(hWnd != NULL);
	ASSERT(simulation != NULL);

	m_simulation = simulation;
	m_hWnd = hWnd;

	// enable OpenGL for the window
	EnableOpenGL(m_hWnd, &m_hDC, &m_hRC );

	myinit();

	//reshape(1024, 768);

	m_dynamicsWorld = simulation->initPhysics(*this);

	if (getDynamicsWorld())
		getDynamicsWorld()->setDebugDrawer(&m_debugDraw);

	enableShowContact(TRUE);
	enableShowAABB(TRUE);

	return true;
}

void Simulator::exit()
{
	m_simulation->exitPhysics();
	m_simulation = NULL;
	m_dynamicsWorld = NULL;

	// shutdown OpenGL
	DisableOpenGL(m_hWnd, m_hDC, m_hRC);
	m_hWnd = NULL;
	m_hDC = NULL;
	m_hRC = NULL;
}

void Simulator::setScreenSize( int x, int y )
{
	reshape(x, y);
}

void Simulator::updateScreen()
{
	// OpenGL animation code goes here
	
	glClearColor( .7f, 0.7f, 0.7f, 1.f );

	moveAndDisplay();

	if(m_hDC != NULL)
		SwapBuffers(m_hDC);
}

void Simulator::displayCallback()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
	
	renderme();

	//optional but useful: debug drawing to detect problems
	if (m_dynamicsWorld)
		m_dynamicsWorld->debugDrawWorld();

	glFlush();

	swapBuffers();
}

void Simulator::clientMoveAndDisplay()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

	//simple dynamics world doesn't handle fixed-time-stepping
	float ms = getDeltaTimeMicroseconds();

	///step the Simulator
	if (m_dynamicsWorld)
	{
		m_dynamicsWorld->stepSimulation(1.0/60.0);
		//m_dynamicsWorld->stepSimulation(ms / 1000000.f);
		//m_dynamicsWorld->stepSimulation(ms / 1000000.f, 1.0, 1.0 / 300.0);

		//optional but useful: debug drawing
		m_dynamicsWorld->debugDrawWorld();
	}

	renderme(); 

	glFlush();

	swapBuffers();
}

void Simulator::initPhysics()
{

}

void Simulator::exitPhysics()
{

}


